package wnhz.com.business.groupshop.adapter;

import android.content.Context;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.gson.Gson;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import wnhz.com.business.R;
import wnhz.com.business.base.BaseRecyclerAdapter;
import wnhz.com.business.base.RecyclerViewHolder;
import wnhz.com.business.groupshop.bean.LogisticsBean;
import wnhz.com.business.impl.ITaskFinishListener;
import wnhz.com.business.uitls.LogUtils;
import wnhz.com.business.uitls.MyCallBack;
import wnhz.com.business.uitls.Prefer;
import wnhz.com.business.uitls.Url;
import wnhz.com.business.uitls.XUtil;

/**
 * Created by Administrator on 2017/6/29 0029.
 */

public class LogisticsAdapter extends BaseRecyclerAdapter<LogisticsBean.ExpressBean> {
    static final String TAG = "NoticeAdapter";

    private String orderId;

    public LogisticsAdapter(Context context, List<LogisticsBean.ExpressBean> datas, String orderId) {
        super(context, datas, R.layout.item_logisyics);
        this.orderId = orderId;
    }

    @Override
    protected void bindViewHolder(int position, RecyclerViewHolder holder) {
        ImageView iv_dian = holder.getView(R.id.iv_dian, false);
        TextView tv_name = holder.getView(R.id.tv_name, false);
        TextView tv_time = holder.getView(R.id.tv_time, false);

        LogisticsBean.ExpressBean item = getItem(position);
        tv_name.setText(item.getAcceptStation());
        tv_time.setText(item.getAcceptTime());

        if(position == (datas.size() - 1)){
            iv_dian.setBackgroundResource(R.drawable.ic_home_logis_selected2x);
        }else{
            iv_dian.setBackgroundResource(R.drawable.ic_home_logis_normal2x);
        }
    }

    @Override
    protected void loadData(int page, final ITaskFinishListener iTaskFinishListener) {
        Map<String, Object> map = new HashMap<>();
        map.put("token", Prefer.getInstance().getToken());
        map.put("orderId", orderId);
        XUtil.Post(Url.ORDER_VIEWLOGISTICS, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                LogUtils.e("=======商品物流详情========", result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if (jsonObject.optString("re").equals("1")) {
                        Gson gson = new Gson();
                        LogisticsBean logisticsBean = gson.fromJson(result, LogisticsBean.class);

                        iTaskFinishListener.onSuccess(TAG, logisticsBean.getExpress());
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
            }
        });
    }


    @Override
    protected void setPageSize(int pageSize) {

    }
}
